package com.china.bike.service.impl;

import com.china.bike.dao.BikeAccountDao;
import com.china.bike.dao.BikeAccountRecordDao;
import com.china.bike.entity.BikeAccount;
import com.china.bike.entity.BikeAccountRecord;
import com.china.bike.entity.User;
import com.china.bike.service.BikeAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

@Service
public class BikeAccountServiceImpl implements BikeAccountService {

    @Autowired
    private BikeAccountDao bikeAccountDao;
    @Autowired
    private BikeAccountRecordDao bikeAccountRecordDao;

    @Override
    public BikeAccount getUserAccount(Long userId) {
        return bikeAccountDao.getUserAccount(userId);
    }

    @Override
    public void recordUseBike(BikeAccount bikeAccount) {
        bikeAccountDao.recordUseBike(bikeAccount);
    }

    @Override
    public void chargeMoney(BigDecimal charge, User user) {
        BikeAccount bikeAccount = bikeAccountDao.getUserAccount(user.getId());
        bikeAccount.setBalance(bikeAccount.getBalance().add(charge));
        bikeAccountDao.recordUseBike(bikeAccount);

        BikeAccountRecord bikeAccountRecord = new BikeAccountRecord();
        bikeAccountRecord.setAccountId(bikeAccount.getAccountId());
        bikeAccountRecord.setAmount(charge);
        bikeAccountRecord.setCreditTime(new Date());
        bikeAccountRecord.setType(1);
        bikeAccountRecord.setUserId(bikeAccount.getUserId());
        bikeAccountRecordDao.createAccountRecord(bikeAccountRecord);
    }
}
